/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-10-12 20:48:39
*/
package com.ueboot.weishop.admin.controller.setting;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.setting.vo.*;
import com.ueboot.weishop.entity.setting.AttrOptionValueEntity;
import com.ueboot.weishop.entity.setting.AttributeEntity;
import com.ueboot.weishop.service.setting.AttrOptionValueService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;


/**
 *  Created by yangkui  on 2017-10-12 20:48:39.
 *
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/attrOptionValue")
public class AttrOptionValueController {

    @Autowired
    private AttrOptionValueService attrOptionValueService;


    @RequiresPermissions("attrOptionValue:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<AttrOptionValueResp>> page(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
                                                     Pageable pageable, @RequestBody(required = false) AttrOptionValueFindReq req){
        Page<AttrOptionValueEntity> entities = attrOptionValueService.findByAttrId(pageable,req.getAttrId());
        Page<AttrOptionValueResp> body = entities.map(entity -> {
            AttrOptionValueResp resp = new AttrOptionValueResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        });

        return new Response<>(body);
    }


    @RequiresPermissions("attrOptionValue:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody AttrOptionValueReq req) {
        AttrOptionValueEntity entity = null;
        if (req.getId() == null) {
            entity = new AttrOptionValueEntity();
        } else {
            entity = attrOptionValueService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);
        Assert.notNull(req.getAttrId());
        AttributeEntity attr  = new AttributeEntity();
        attr.setId(req.getAttrId());
        entity.setAttr(attr);
        attrOptionValueService.save(entity);
        return new Response<>();
    }

    @RequiresPermissions("attrOptionValue:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        attrOptionValueService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("attrOptionValue:read")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<AttrOptionValueResp> get(@PathVariable Long id) {
        AttrOptionValueEntity entity = attrOptionValueService.get(id);
        AttrOptionValueResp resp = new AttrOptionValueResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

}
